﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Carbon.Web.Forms.jQueryForms
{
    public class BooleanEditor : CheckBox, IEditor, IScriptControl
    {
        #region IDataEditingControl Members

        public bool IsNull
        {
            get
            {
                return false;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            this.Editor.ScriptManager.RegisterScriptControl(this);
            this.Editor.ScriptManager.RegisterScriptDescriptors(this);

            this.AutoPostBack = Editor.AutoPostback;
            this.Text = Editor.LabelText;
        }

        private SimpleEditor m_Editor = default(SimpleEditor);
        public SimpleEditor Editor
        {
            get
            {
                return m_Editor;
            }
        }

        protected override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            bool result = base.LoadPostData(postDataKey, postCollection);

            if (!Editor.AutoPostback)
            {
                m_Value = this.Checked;
            }
            else
            {
                m_StoredValue = this.Checked;
            }

            return result;
        }

        protected override void RaisePostDataChangedEvent()
        {
            base.RaisePostDataChangedEvent();

            if (Editor.AutoPostback)
            {
                m_Value = m_StoredValue;
                this.RaiseBubbleEvent(this, new UpdateModelEventArgs());
            }
        }

        private bool m_StoredValue = false;
        private bool m_Value = false;
        public virtual object Value
        {
            get
            {
                return m_Value;
            }
            set
            {
                m_Value = (bool?)value ?? false;
                this.Checked = m_Value;
            }
        }

        #endregion

        #region IScriptControl Members

        private class BooleanEditorScriptDescriptor : ScriptDescriptor
        {
            private BooleanEditor m_Editor = default(BooleanEditor);
            public BooleanEditor Editor
            {
                get
                {
                    return m_Editor;
                }
            }

            public BooleanEditorScriptDescriptor(BooleanEditor editor)
                : base()
            {
                if (editor == null) throw new ArgumentNullException("editor");

                this.m_Editor = editor;
            }

            protected override string GetScript()
            {
                return Editor.GetClientScript();
            }
        }

        protected virtual String GetClientScript()
        {
            return string.Empty;
        }

        public virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            return new ScriptDescriptor[] { new BooleanEditorScriptDescriptor(this) };
        }

        public virtual IEnumerable<ScriptReference> GetScriptReferences()
        {
            return new ScriptReference[] { };
        }

        public BooleanEditor(SimpleEditor editor)
            : base()
        {
            if (editor == null) throw new ArgumentNullException("editor");

            this.m_Editor = editor;
        }

        #endregion
    }
}
